/**
 * Copyright (c) 2013, Redsolution LTD. All rights reserved.
 * 
 * This file is part of Xabber project; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License, Version 3.
 * 
 * Xabber is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License,
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */
package com.digitalbuana.smiles.service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.digitalbuana.smiles.data.AppConstants;
import com.digitalbuana.smiles.data.Application;
import com.digitalbuana.smiles.data.LogManager;
import com.digitalbuana.smiles.data.connection.ConnectionManager;
import com.digitalbuana.smiles.data.notification.NotificationManager;

public class SmilesService extends Service{

	private Method startForeground;
	private Method stopForeground;

	private static SmilesService instance;

	private MyPhoneStateListener MyListener;
	private TelephonyManager Tel;
	private Context cntx;
	private SharedPreferences mSettings;
	private SharedPreferences.Editor settingsEditor;
	private String MY_CONNECTION_MODE = "connection_mode";
	private String MY_IP_KEY = "my_ip_key";
	
	
	public static SmilesService getInstance() {
		return instance;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		instance = this;
		LogManager.i(this, "onCreate");

		// Try to get methods supported in API Level 5+
		try {
			startForeground = getClass().getMethod("startForeground",
					new Class[] { int.class, Notification.class });
			stopForeground = getClass().getMethod("stopForeground",
					new Class[] { boolean.class });
		} catch (NoSuchMethodException e) {
			startForeground = stopForeground = null;
		}

		changeForeground();
	}

	public void changeForeground() {
//		if (SettingsManager.eventsPersistent()
//				&& Application.getInstance().isInitialized())
//			startForegroundWrapper(NotificationManager.getInstance()
//					.getPersistentNotification());
//		else
			stopForegroundWrapper();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		Application.getInstance().onServiceStarted();
		cntx = Application.getInstance().getApplicationContext();
		mSettings = PreferenceManager.getDefaultSharedPreferences(cntx);
		settingsEditor = mSettings.edit();
		MyListener   = new MyPhoneStateListener();
        Tel       = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        Tel.listen(MyListener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
        tryToTriggerHeartBeatConnection();
//		Application.getInstance().addUIListener(OnTimerListener.class, this);
//		Application.getInstance().addUIListener(OnChatChangedListener.class, this);
		
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		LogManager.i(this, "onDestroy");
		stopForegroundWrapper();
		Application.getInstance().onServiceDestroy();
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	/**
	 * This is a wrapper around the new startForeground method, using the older
	 * APIs if it is not available.
	 */
	void startForegroundWrapper(Notification notification) {
		if (startForeground != null) {
			Object[] startForegroundArgs = new Object[] {
					Integer.valueOf(NotificationManager.PERSISTENT_NOTIFICATION_ID),
					notification };
			try {
				startForeground.invoke(this, startForegroundArgs);
			} catch (InvocationTargetException e) {
				// Should not happen.
				LogManager.w(this, "Unable to invoke startForeground" + e);
			} catch (IllegalAccessException e) {
				// Should not happen.
				LogManager.w(this, "Unable to invoke startForeground" + e);
			}
		} else {
//			setForeground(true);
			try {
				((android.app.NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(NotificationManager.PERSISTENT_NOTIFICATION_ID,
								notification);
			} catch (SecurityException e) {
			}
		}
	}

	/**
	 * This is a wrapper around the new stopForeground method, using the older
	 * APIs if it is not available.
	 */
	void stopForegroundWrapper() {
		if (stopForeground != null) {
			try {
				stopForeground.invoke(this, new Object[] { Boolean.TRUE });
				// We don't want to clear notification bar.
			} catch (InvocationTargetException e) {
				// Should not happen.
				LogManager.w(this, "Unable to invoke stopForeground" + e);
			} catch (IllegalAccessException e) {
				// Should not happen.
				LogManager.w(this, "Unable to invoke stopForeground" + e);
			}
		} else {
//			setForeground(false);
		}
	}

	public static Intent createIntent(Context context) {
		return new Intent(context, SmilesService.class);
	}

	
	private class MyPhoneStateListener extends PhoneStateListener{		
		@SuppressLint("NewApi")
		public void onSignalStrengthsChanged(SignalStrength signalStrength){			
	         super.onSignalStrengthsChanged(signalStrength);	         
	         ConnectivityManager cm = 
	 	        (ConnectivityManager) cntx.getSystemService(Context.CONNECTIVITY_SERVICE);	         
	         if (cm != null) {	        	 
	 	        NetworkInfo[] netInfo = cm.getAllNetworkInfo();	 	       
	 	        for (NetworkInfo ni : netInfo) {	 	        	
	 	        	String myConnectionMode = ni.getTypeName();	 	        	
	 	            if ((myConnectionMode.equalsIgnoreCase("WIFI")
	 	                    || myConnectionMode.equalsIgnoreCase("MOBILE"))
	 	                    && ni.isConnected() && ni.isAvailable()) {	 	            	
	 	            	String myCurrentConnectionMode = mSettings.getString(MY_CONNECTION_MODE, null);	 	            	
	 	            	String existingConnectionMode = null;	 	            	
	 	            	if( myCurrentConnectionMode == null ){	 	            		
	 	            		if(myConnectionMode.equalsIgnoreCase("WIFI")){
	 	            			settingsEditor.putString(MY_CONNECTION_MODE, myConnectionMode);
	 	            			existingConnectionMode = myConnectionMode;
	 	            		}else{
	 	            			settingsEditor.putString(MY_CONNECTION_MODE, ni.getSubtypeName());
	 	            			existingConnectionMode = ni.getSubtypeName();
	 	            		}	 	            		
	 	            		settingsEditor.commit();	 	            		
	 	            	}else{
	 	            		if(myConnectionMode.equalsIgnoreCase("WIFI")){
	 	            			existingConnectionMode = myConnectionMode;
	 	            		}else{
	 	            			existingConnectionMode = ni.getSubtypeName();
	 	            		}
	 	            	}	 	            	
	 	            	String myIp = getLocalIpAddress();	 	            	
	 	            	String myLocalIp = mSettings.getString(MY_IP_KEY, null);	 	            	
	 	            	if(myLocalIp == null){	 	            		
	 	            		settingsEditor.putString(MY_IP_KEY, myIp);
	 	            		settingsEditor.commit();	 	            		
	 	            	}else if(!myLocalIp.equalsIgnoreCase(myIp)){
	 	            		settingsEditor.putString(MY_IP_KEY, myIp);
	 	            		settingsEditor.commit();
	 	            		tryToTriggerHeartBeatConnection();
	 	            		
	 	            	}else{	 	            		
	 	            		if(!myCurrentConnectionMode.equals(existingConnectionMode)){ 	 	            		
	 	 	            		settingsEditor.putString(MY_CONNECTION_MODE, existingConnectionMode);
	 	 	            		settingsEditor.commit();
	 	 	            		tryToTriggerHeartBeatConnection();
	 	 	            	}
	 	            	}
	 	            }
	 	        }
	 	    }
		}
	}
	private void tryToTriggerHeartBeatConnection(){
		Log.i(AppConstants.TAG, "tryToTriggerHeartBeatConnection");
		ConnectionManager.getInstance().checkOnTimer();
	}
	
	public static String getLocalIpAddress() {
	    try {
	        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
	            NetworkInterface intf = en.nextElement();
	            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
	                InetAddress inetAddress = enumIpAddr.nextElement();
	                if (!inetAddress.isLoopbackAddress()) {
	                    return inetAddress.getHostAddress().toString();
	                }
	            }
	        }
	    } catch (SocketException ex) {
	        Log.e(AppConstants.TAG, ex.toString());
	    }
	    return null;
	}
}
